/**
 * 
 */
package org.intelligentsia.artifacts.repository.metadata.adapter;

import org.json.JSONException;
import org.json.JSONObject;

import org.intelligentsia.artifacts.repository.metadata.Metadata;

/**
 * @author geronimo
 * 
 */
public class MetadataAdater {

	public static Metadata read(JSONObject jsonObject) throws JSONException {
		Metadata metadata = new Metadata();
		metadata.setGroupId(jsonObject.getString("groupId"));
		metadata.setArtifactId(jsonObject.getString("artifactId"));
		metadata.setVersion(jsonObject.getString("version"));
		metadata.setVersioning(VersioningAdapter.read(jsonObject.optJSONObject("versioning")));
		return metadata;
	}

	public static JSONObject write(Metadata metadata) throws JSONException {
		JSONObject jsonObject = null;
		if (metadata != null) {
			jsonObject = new JSONObject();
			jsonObject.putOpt("groupId", metadata.getGroupId());
			jsonObject.putOpt("artifactId", metadata.getArtifactId());
			jsonObject.putOpt("version", metadata.getVersion());
			jsonObject.putOpt("versioning", VersioningAdapter.write(metadata.getVersioning()));
		}
		return jsonObject;
	}

}
